package demo3;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-13
 * Time: 19:34
 */
//多态
class Animal{
    private String name;
    private int age;

    public Animal(String name, int age) {
        this.name = name;
        this.age = age;
        //eat();->也可以发生所谓的动态绑定,以后不要这样写代码
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public void eat(){
        System.out.println(name+" 吃东西(animal)");
    }

    @Override
    public String toString() {
        return "Animal{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class Dog extends Animal{

    public Dog(String name, int age) {
        super(name, age);
    }

    public void bark(){
        System.out.println(getName()+" 汪汪汪");
    }

    @Override
    public void eat() {
        System.out.println(getName()+" 吃狗粮");
    }
}
class Bird extends Animal{

    public String wing;
    public Bird(String name, int age) {
        super(name, age);
    }

    public void fly(){
        System.out.println(getName()+" 正在飞");
    }

    @Override
    public void eat() {
        System.out.println(getName()+" 要吃鸟粮");
    }
}
public class Test {

    public static void main7(String[] args) {
        Animal animal = new Animal("动物",10);
//        Animal animal = new Dog("狗子",3);
        animal.eat();
    }
    public static void main6(String[] args) {
        Animal animal = new Dog("狗子",3);
        if(animal instanceof Bird){
            //不是所有的动物都是鸟
            Bird bird = (Bird)animal;
            bird.fly();
        }
    }
    public static void main5(String[] args) {
        Animal animal = new Bird("鸟儿",3);
        Bird bird = (Bird)animal;//向下转型
        bird.fly();
    }
    public static void func1(Animal animal){
        animal.eat();
    }

    public static void main4(String[] args) {
        func1(new Bird("鸟儿",3));
        func1(new Dog("狗子",6));
    }



    public static Animal func2(){
        return new Bird("鹦鹉",2);
    }
    public static void main3(String[] args) {
        Bird bird = new Bird("鹦鹉",2);
        func1(bird);

    }
    public static void main2(String[] args) {
//        Bird bird = new Bird("小鸟",3);
//        Animal animal = bird;
        //父类的引用  引用了子类的对象
        Animal animal = new Bird("鹦鹉",2);
        animal.eat();//动态绑定
//        animal.fly();//不可以调用了  此时通过父类引用，只能调用自己特有的方法，不可以调用子类方法
//        animal.wing;
    }
    public static void main1(String[] args) {
        Bird bird = new Bird("小鸟",3);
        bird.fly();
        bird.eat();
    }
}
